package com.hu.zhcc.shiro.controller.dingming;

import com.hu.zhcc.common.entity.Page;
import com.hu.zhcc.shiro.entity.dingming.TMemberExamination_D;
import com.hu.zhcc.shiro.entity.dingming.TOrderInfo;
import com.hu.zhcc.shiro.service.dingming.TMemberExaminationService;
import com.hu.zhcc.shiro.service.dingming.TOrderInfoService;
import com.hu.zhcc.shiro.service.dingming.TOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/tOrderInfo")
public class TOrderInfoController {
    @Autowired
    private TOrderInfoService tOrderInfoService;
    @Autowired
    private TOrderService tOrderService;
    @Autowired
    private TMemberExaminationService tMemberExaminationService;
    /**
     * 添加预约
     * @param tOrderInfo
     * @param comboIds
     * @return
     */
    @PostMapping("/{comboIds}")
    public ResponseEntity<TOrderInfo> saveOrder(@RequestBody TOrderInfo tOrderInfo, @PathVariable Integer[] comboIds) {
        tOrderInfo.getTOrder().setOrderdate(tOrderInfo.getTime());
        for (Integer comboId : comboIds) {
            tOrderInfo.setSetmealId(comboId);
            tOrderInfo.getTOrder().setSetmealId(comboId);
            tOrderService.insertReturnKey(tOrderInfo.getTOrder());
            System.out.println("tOrderId=" + tOrderInfo.getTOrder());
            tOrderInfo.setOrderId(tOrderInfo.getTOrder().getId());
            tOrderInfoService.insert(tOrderInfo);
        }
        TMemberExamination_D tMemberExaminationD =new TMemberExamination_D();
        tMemberExaminationD.settSetmealId(comboIds[0]);
        tMemberExaminationD.settMemberId(tOrderInfo.getMemberId());
        tMemberExaminationD.setCreatetime(new Date());
        tMemberExaminationService.insert(tMemberExaminationD);
        return ResponseEntity.status(HttpStatus.CREATED).body(tOrderInfo);
    }

    /**
     * 分页展示预约数据
     * @param name
     * @param orderStatus
     * @param offset
     * @param limit
     * @return
     */
    @GetMapping("")
    public ResponseEntity<Page<TOrderInfo>> getOrderList(@RequestParam(required = false) String name,
                                                         @RequestParam(required = false) Integer orderStatus,
                                                         int offset, int limit) {
        Map<String, Object> paremeters = new HashMap<String, Object>();
        paremeters.put("name", name);
        paremeters.put("orderStatus", orderStatus);
        Page<TOrderInfo> result = tOrderInfoService.listTOrderInfo(paremeters, offset, limit);
        return ResponseEntity.ok(result);
    }

    @PutMapping("/{orderId}/{orderStatus}")
    public ResponseEntity<Integer> changeStatus(@PathVariable() Integer orderId,@PathVariable() Integer orderStatus) {
        System.out.println("orderId="+orderId+";orderStatus="+orderStatus);
        Map<String,Integer> map=new HashMap<>();
        map.put("orderId",orderId);
        map.put("orderStatus",orderStatus);
        System.out.println(map);
        int rows = tOrderService.changeStatus(map);
        if (rows == 0) {
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.status(HttpStatus.CREATED).body(rows);
    }

}

